package com.changgou.goods.controller;

import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.TestPropertySource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author seafwg
 * @Date 2021/11/21 2:50 下午
 * @Version 1.0
 * @description xxx
 */
@RestController
@RequestMapping(value = "/brand")
@CrossOrigin
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 根据分类ID查询商品：
     * @param categoryId
     * @return
     */
    @GetMapping(value = "/categoryId")
    public Result<List<Brand>> findBrandByCategory(@PathVariable(name = "categoryId") Integer categoryId) {
        List<Brand> brand = brandService.findBrandByCategoryId(categoryId);
        return new Result<>(true, StatusCode.OK, "查询成功", brand);
    }

    /**
     * 查询所有brand
     * @return
     */
    @GetMapping
    public Result<List<Brand>> findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result<List<Brand>>(true, StatusCode.OK, "查询所有brand成功！", brandList);
    }

    /**
     * 根据品牌ID查询商品
     * @param id
     * @return
     * // @PathVariable(name="xxx") ... 代表从所有的变量中拿到xxx的变量
     */
    @GetMapping(value = "/{id}")
    public Result<Brand> findBrandById(@PathVariable(name = "id") Integer id) {
        Brand byId = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK, "查询id的brand成功！", byId);
    }

    /**
     * 增加商品成功
     * @param brand
     * @return
     * // @RequestBody：表示接受传递一个对象
     */
    @PostMapping
    public Result addBrand(@RequestBody Brand brand) {
        brandService.addBrand(brand);
        return new Result(true, StatusCode.OK, "增加商品成功！");
    }

    /**
     * 根据ID修改商品信息
     * @param id
     * @param brand
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result updateBrandById(@PathVariable(name = "id") Integer id,  @RequestBody Brand brand) {
        brand.setId(id);
        brandService.updateBrandById(brand);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /**
     * 根据商品ID删除商品
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result deleteBrandById(@PathVariable(name = "id") Integer id) {
        brandService.deleteBrandById(id);
        return new Result(true, StatusCode.OK, "删除商品成功！");
    }

    /**
     * 条件查询brand
     * @param brand
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Brand>> findBrandList(@RequestBody Brand brand) {
        List<Brand> brandList = brandService.findBrandList(brand);
        return new Result<List<Brand>>(true, StatusCode.OK, "查询成功！", brandList);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable(name = "page") Integer page, @PathVariable(name = "size") Integer size) {
        PageInfo<Brand> brandPage = brandService.findPage(page, size);
        return new Result<PageInfo<Brand>>(true, StatusCode.OK, "分页查询成功！", brandPage);
    }

    /**
     * 有条件的分页查询：
     * @param brand
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(
            @RequestBody Brand brand,
            @PathVariable(name = "page") Integer page,
            @PathVariable(name = "size") Integer size
    ) {
        PageInfo<Brand> brandPageList = brandService.findPage(brand, page, size);
        return new Result<PageInfo<Brand>>(true, StatusCode.OK, "条件分页查询成功！", brandPageList);
    }
}
